using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using SlimDX.Direct3D9;

namespace miciv.Rendering
{
    public abstract class VertexFeederBase<T, I> : IVertexFeeder
        where T : struct
        where I : struct
    {
        protected PrimitiveType m_primitiveType;
        public PrimitiveType PrimitiveType
        {
            get
            {
                return this.m_primitiveType;
            }
            set
            {
                this.m_primitiveType = value;
            }
        }

        protected int m_sPrimitiveSize;
        public int PrimitiveSize
        {
            get
            {
                return this.m_sPrimitiveSize;
            }
            set
            {
                this.m_sPrimitiveSize = value;
            }
        }

        protected ManagedVertexBuffer<T> m_vertexBuffer;
        public ManagedVertexBuffer<T> VertexBuffer
        {
            get
            {
                return this.m_vertexBuffer;
            }
        }

        protected ManagedIndexBuffer<I> m_indexBuffer;
        public ManagedIndexBuffer<I> IndexBuffer
        {
            get
            {
                return this.m_indexBuffer;
            }
        }

        public VertexFeederBase(ManagedVertexBuffer<T> _vertexBuffer, ManagedIndexBuffer<I> _indexBuffer)
        {
            this.m_vertexBuffer = _vertexBuffer;
            this.m_indexBuffer = _indexBuffer;
            this.PrimitiveType = PrimitiveType.PointList;
            this.PrimitiveSize = 0;
        }

        #region IVertexFeeder Members

        virtual public int VertexCount
        {
            get
            {
                return this.m_vertexBuffer.VertexCount;
            }
        }

        virtual public int VertexOffset
        {
            get
            {
                return this.m_vertexBuffer.Offset;
            }
        }

        virtual public bool VertexLock()
        {
            return true;
        }

        abstract public void SetVertex(uint _vertexIndex, Hashtable _htblData);

        virtual public void VertexUnlock()
        {
            this.m_vertexBuffer.IsDirty = true;
        }

        virtual public int IndexCount
        {
            get
            {
                if (null != this.m_indexBuffer)
                {
                    return this.m_indexBuffer.IndexCount;
                }
                return 0;
            }
        }

        virtual public int IndexOffset
        {
            get
            {
                if (null != this.m_indexBuffer)
                {
                    return this.m_indexBuffer.Offset;
                }
                return -1;
            }
        }

        virtual public bool IndexLock()
        {
            return true;
        }

        virtual public void SetIndex(uint _index, object _vertexIndex)
        {
            if (this.IndexCount > _index)
            {
                this.m_indexBuffer.Indices[_index] = (I)_vertexIndex;
            }
        }

        virtual public void IndexUnlock()
        {
            this.m_indexBuffer.IsDirty = true;
        }

        #endregion
    }
}
